package org.gdstash.db;

import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class DBSkillExperience {
    private static final String TABLE_NAME = "GD_SKILL_EXPERIENCE";
    private static final int ROW_SKILL_ID = 1;
    private static final int ROW_EXPERIENCE = 2;

    public static void createTable(final Connection conn) throws SQLException {
        final String dropTable = "DROP TABLE GD_SKILL_EXPERIENCE";
        final String createTable = "CREATE TABLE GD_SKILL_EXPERIENCE (SKILL_ID     VARCHAR(256) NOT NULL, EXPERIENCE   INTEGER, PRIMARY KEY ( SKILL_ID, EXPERIENCE))";
        try (final Statement st = conn.createStatement()) {
            if (GDDBUtil.tableExists(conn, "GD_SKILL_EXPERIENCE")) {
                st.execute(dropTable);
            }
            st.execute(createTable);
            st.close();
            conn.commit();
        } catch (SQLException ex) {
            final Object[] args = {"GD_SKILL_EXPERIENCE"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_CREATE_TABLE", args);
            GDMsgLogger.addError(msg);
            throw ex;
        }
    }

    public static void delete(final Connection conn, final String skillID) throws SQLException {
        final String deleteEntry = "DELETE FROM GD_SKILL_EXPERIENCE WHERE SKILL_ID = ?";
        try (final PreparedStatement ps = conn.prepareStatement(deleteEntry)) {
            ps.setString(1, skillID);
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public static void insert(final Connection conn, final String skillID, final List<Integer> xpLevels) throws SQLException {
        final String insert = "INSERT INTO GD_SKILL_EXPERIENCE VALUES (?,?)";
        if (xpLevels == null) {
            return;
        }
        if (xpLevels.isEmpty()) {
            return;
        }
        try (final PreparedStatement ps = conn.prepareStatement(insert)) {
            for (final int i : xpLevels) {
                ps.setString(1, skillID);
                ps.setInt(2, i);
                ps.executeUpdate();
                ps.clearParameters();
            }
            ps.close();
            conn.commit();
        }
    }

    public static List<Integer> getBySkillID(final String skillID) {
        List<Integer> list = new LinkedList<Integer>();
        final String command = "SELECT * FROM GD_SKILL_EXPERIENCE WHERE SKILL_ID = ? ORDER BY EXPERIENCE";
        try (final Connection conn = GDDBData.getConnection();
             final PreparedStatement ps = conn.prepareStatement(command)) {
            ps.setString(1, skillID);
            try (final ResultSet rs = ps.executeQuery()) {
                list = wrap(rs);
                conn.commit();
            } catch (SQLException ex) {
                throw ex;
            }
        } catch (SQLException ex2) {
            final Object[] args = {skillID, "GD_SKILL_EXPERIENCE"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_TABLE_BY_ID", args);
            GDMsgLogger.addError(msg);
            GDMsgLogger.addError(ex2);
        }
        return list;
    }

    private static List<Integer> wrap(final ResultSet rs) throws SQLException {
        final LinkedList<Integer> list = new LinkedList<Integer>();
        while (rs.next()) {
            final int i = rs.getInt(2);
            final Integer val = i;
            list.add(val);
        }
        return list;
    }
}
